<?php
namespace Admin\Controller;

class RechargeController extends Controller {

    public function index() {
        $m    = D('VRecharge');
        $page = $m->where($this->indexExpr)->order($this->sortExpr)->page2();

        $this->assign('page', $page);
        $this->display();
    }

    public function modify() {
        $id = empty($_GET['id']) ? 0 : (int)$_GET['id'];

        if(empty($id)) {
            throw new ThinkException('参数不完整，无法获取数据');
        }

        $m = D2();

        if(!$record = $m->getByZoneId($id)) {
            throw new ThinkException('没有找到广告位信息');
        }

        $mAd = D2('Ad');
        $rAd = $mAd->getAdsByTypeAndSize($record['zone_type'], $record['zone_width'], $record['zone_height']);

        $this->assign('ad', $rAd);
        $this->assign('record', $record);
        $this->display();
    }

    public function doSave() {
        $dao = [            
            'user_name'  => I('post.user_name',  '',  'trim'),
            'money'      => I('post.money',      0,   'floatval'),
            'money_type' => I('post.money_type', 1, 'intval'),
            'type'       => I('post.type',       1, 'intval'),
            'status'     => I('post.type',       1, 'intval'),
            'comment'    => I('post.comment',    ''),
            'gateway'    => C('RECHARGE_DEFAULT_GATEWAY'),
        ];

        try {
            $r = D2()->save2($dao) 
                 ? ['s' => true,  'm' => '保存成功']
                 : ['s' => false, 'm' => '保存出错'];
        } catch (Exception $e) {
            $r = ['s' => false, 'm' => $e->getMessage()];
        }

        showJson($r);
    }

    public function getById() {
        $id = I('get.id', 0, 'intval');

        if(empty($id)) {
            showJson(['s' => false, 'm' => '参数不完整，无法获取数据']);
        }

        $m = D2();

        if (!$record = $m->where(['id' => $id])->find()) {
            showJson(['s' => false, 'm' => '没有找到相关数据']);
        }

        if (!$userRecord = D('User')->where(['id' => $record['uid']])->find()) {
            showJson(['s' => false, 'm' => '没有找到相应会员信息']);
        }

        $record['user_name'] = $userRecord['name'];

        showJson(['s' => true, 'm' => $record]);
    }

    /**
     * doDeduct
     * 针对预充值的扣除操作
     *
     * @return void
     */
    public function doDeduct() {
        $dao = array(
            'id'      => I('post.id', 0, 'intval'),
            'comment' => I('post.comment', ''),
        );
        $m = D2();

        try {
            $msg = $m->saveDeduction($dao) 
                   ? ['s' => true,  'm' => '扣除成功']
                   : ['s' => false, 'm' => '扣除出错'];
        } catch(Exception $e) {
            $msg = ['s' => false, 'm' => $e->getMessage()];
        }

        showJson($msg);
    }

    /**
     * doCleanRecharge
     * 清理本月之前的所有除预充值成功之外的所有充值数据
     *
     * @return void
     */
    public function doCleanRecharge() {
        $day  = date('Y-m-01');
        $expr = "r_create_time < '{$day}' AND r_status<> 5";

        D2()->where($expr)->delete();

        showJson(array('s' => true, 'm' => '清理成功'));
    }

}
